package com.cloudspace.jindun.activity.interphone;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.android.volley.error.VolleyError;
import com.cloudspace.jindun.R;
import com.cloudspace.jindun.activity.CustomTitleBaseActivity;
import com.cloudspace.jindun.adapter.MyChannelAdapter;
import com.cloudspace.jindun.model.ChannelInfo;
import com.cloudspace.jindun.model.ChannelListResult;
import com.cloudspace.jindun.module.ActionItem;
import com.cloudspace.jindun.network.api.API;
import com.cloudspace.jindun.network.base.ApiCallback;
import com.cloudspace.jindun.utils.ObjUtil;
import com.cloudspace.jindun.utils.ToastMaker;

import java.util.List;

/**
 * Created by zengxianhua on 17/2/10.
 */

public class MyChannelActivity extends CustomTitleBaseActivity{
    public static final String TAG = MyChannelActivity.class.getSimpleName();

    private ListView mMyChannelListView;
    private MyChannelAdapter mMyChannelAdapter;
    private View mChannelEmptyView;
    private List<ChannelInfo> mChannelData;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.duijiang_channel_my, true);

        getTitleBar().setSimpleMode(getResources().getString(R.string.channel_my),
                new ActionItem(R.drawable.channel_add, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                InterPhoneCreateActivity.showActivity(MyChannelActivity.this);
            }
        }));

        mMyChannelAdapter = new MyChannelAdapter();
        mMyChannelListView = (ListView) findViewById(R.id.duijiang_lv_channel_my);
        mMyChannelListView.setAdapter(mMyChannelAdapter);
        mChannelEmptyView = findViewById(R.id.channelEmptyView);
        mMyChannelListView.setEmptyView(mChannelEmptyView);
        mMyChannelListView.getEmptyView().setVisibility(View.GONE);

        mMyChannelListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                if (!ObjUtil.isEmpty(mChannelData)){
                    ChannelInfo info = mChannelData.get(position);

                    if (info != null){
                        MyChannelDetailActivity.showActivity(MyChannelActivity.this, info.channelId);
                    }
                }

            }
        });
    }

    @Override
    protected void onStart() {
        super.onStart();
        API.getInstance().getChannelApi().getMyChannel(TAG, new ApiCallback<ChannelListResult>() {
            @Override
            public void handleResult(ChannelListResult result, VolleyError error) {
                if (error == null){
                    mChannelData = result.data;
                    mMyChannelAdapter.setDatas(result.data);
                }else{
                    ToastMaker.showToastShort(error.getMessage());
                }
                if (ObjUtil.isEmpty(mChannelData)){
                    mMyChannelListView.getEmptyView().setVisibility(View.VISIBLE);
                }
            }
        });
    }

    public static void showActivity(Context context){
        Intent intent = new Intent(context, MyChannelActivity.class);
        if (!(context instanceof Activity)){
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        }

        context.startActivity(intent);
    }
}
